package cn.community.care.module.system.service.info;

import cn.community.care.module.system.controller.admin.info.vo.InfoPageReqVO;
import cn.community.care.module.system.controller.admin.info.vo.InfoRespVO;
import cn.community.care.module.system.controller.admin.info.vo.InfoSaveReqVO;
import cn.community.care.module.system.dal.dataobject.info.InfoDO;
import cn.community.care.module.system.dal.dataobject.user.AdminUserDO;
import cn.community.care.module.system.dal.mysql.info.InfoMapper;
import cn.community.care.module.system.dal.mysql.user.AdminUserMapper;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;


import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 房间信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;

    @Resource
    private AdminUserMapper userMapper;

    @Override
    public Long createInfo(InfoSaveReqVO createReqVO) {
        // 插入
        InfoDO info = BeanUtils.toBean(createReqVO, InfoDO.class);
        infoMapper.insert(info);
        // 返回
        return info.getId();
    }

    @Override
    public void updateInfo(InfoSaveReqVO updateReqVO) {
        // 校验存在
        validateInfoExists(updateReqVO.getId());
        // 更新
        InfoDO updateObj = BeanUtils.toBean(updateReqVO, InfoDO.class);
        infoMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfo(Long id) {
        // 校验存在
        validateInfoExists(id);
        // 删除
        infoMapper.deleteById(id);
    }

    private void validateInfoExists(Long id) {
        if (infoMapper.selectById(id) == null) {
//            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public InfoDO getInfo(Long id) {
        InfoDO infoDO = infoMapper.selectById(id);
        if (Objects.equals(infoDO.getType(), "1")){
            InfoDO infoDO1 = infoMapper.selectOne(InfoDO::getId, infoDO.getParentNumber());
            infoDO.setBuildingNumber(infoDO1.getBuildingNumber());
        }
        if (Objects.equals(infoDO.getType(), "2")){
            InfoDO infoDO1 = infoMapper.selectOne(InfoDO::getId, infoDO.getBuildingNumber());
            infoDO.setBuildingNumber(infoDO1.getBuildingNumber());
            InfoDO infoDO2  = infoMapper.selectOne(InfoDO::getId, infoDO.getFloorNumber());
            infoDO.setFloorNumber(infoDO2.getFloorNumber());
        }
        return infoDO;
    }

    @Override
    public PageResult<InfoRespVO> getInfoPage(InfoPageReqVO pageReqVO) {
        PageResult<InfoDO> infoDOPageResult = infoMapper.selectPage(pageReqVO);
        PageResult<InfoRespVO> infoRespVOPageResult = new PageResult<>();
        infoRespVOPageResult.setTotal(infoDOPageResult.getTotal());
        List<InfoDO> list = infoDOPageResult.getList();
        List<InfoRespVO> voList = BeanUtils.toBean(list, InfoRespVO.class);
        voList.forEach(item -> {
            if (Objects.equals(item.getType(), "1")){
                InfoDO infoDO = infoMapper.selectOne(InfoDO::getId, item.getParentNumber());
                item.setBuildingNumber(infoDO.getBuildingNumber());
            }
            if (Objects.equals(item.getType(), "2")){
                InfoDO infoDO1 = infoMapper.selectOne(InfoDO::getId, item.getBuildingNumber());
                item.setBuildingNumber(infoDO1.getBuildingNumber());
                InfoDO infoDO2 = infoMapper.selectOne(InfoDO::getId, item.getFloorNumber());
                item.setFloorNumber(infoDO2.getFloorNumber());
            }
            if (item.getUserId()!=null){
                AdminUserDO adminUserDO = userMapper.selectById(item.getUserId());
                item.setUserName(adminUserDO.getNickname());
            }
        });
        infoRespVOPageResult.setList(voList);
        return infoRespVOPageResult;
    }

}